<template>
	<view>
		<view>
			<table-title content="装修服务办理表详情"></table-title>
			<view class="record-id">单号：{{processNo}}</view>
			<view class="in-center">
				<small-title content="基本资料"></small-title>
				<view class="state-bar">
					<view class="state-msg">{{state}}</view>
				</view>
			</view>
			<view class="item-table">
				<view-item name="楼盘" :value="userMsg.userBuilding"></view-item>
				<view-item name="楼层" :value="userMsg.userFloor" :isLast="true"></view-item>
			</view>
			<view>
				<small-title :isNeed="true" content="是否自行装修"></small-title>
				<radio-group class="radiogroup-style" :disabled="!(state === '待申请' || status===13)">
					<view class="first-radio">
						<radio @click="chooseWork" :disabled="!(state === '待申请' || status===13)" :checked="!publicChecked" color="#FF0000"></radio>
						<view>自行装修</view>
					</view>
					<view class="second-radio">
						<radio @click="choosePublic" :disabled="!(state === '待申请' || status===13)" :checked="publicChecked" color="#FF0000"></radio>
						<view>否</view>
					</view>
				</radio-group>
			</view>
<!-- 			<view v-if="publicChecked">
				<small-title content="是否自行设计"></small-title>
				<radio-group class="radiogroup-style" :disabled="!(state === '待申请' || status===13)">
					<view class="first-radio">
						<radio @click="chooseSelf" :disabled="!(state === '待申请' || status===13)" :checked="selfChecked" color="#FF0000"></radio>
						<view>自行设计</view>
					</view>
					<view class="second-radio">
						<radio @click="chooseOther" :disabled="!(state === '待申请' || status===13)" :checked="!selfChecked" color="#FF0000"></radio>
						<view>否</view>
					</view>
				</radio-group>
			</view> -->

			<view v-if="(publicChecked && (status == 2 || (status > 4 && (status != 13 && !selfChecked)))) || (!publicChecked)">
				<small-title :isNeed="true" content="上传平面设计图"></small-title>
				<view class="img-repair">
					<uni-file-picker 
						v-model="imageValue"  
						file-mediatype="image"
						mode="grid"
						file-extname="png,jpg"
						:limit="9"
						ref="files"
						:auto-upload="false"
						:readonly="!(state === '待申请')"
						@select="select"
						@delete="deleteImg"
					/>
				</view>
			</view>
			<!-- <con-advance-pay-small-title v-if="state==='待支付'" content="支付方式"></con-advance-pay-small-title> -->
			<view v-if="status > 7 && status < 11" class="small-title">支付信息截图</view>
			<image v-if="status > 7 && status < 11" :class="chargeImg=='/static/uploadImg.png' ? 'pay-image' : 'pay-image-border'" :src="chargeImg" mode="scaleToFill" @tap='chooseChargeImg'></image>
			<small-title content="处理进度"></small-title>
			<proccess-bar  v-if="processFlag" :proccessList="proccessListDefult"></proccess-bar>
			<view v-if="status===5" class="mix-button">
				<view class="left-part">
					<button class="left-button" @click="confirmDrawing">确认设计图</button>
				</view>
				<view class="right-part">
					<button class="right-button" @click="cancelOrder">取消装修</button>
				</view>
			</view>
			<view v-if="status===9" class="mix-button">
				<view class="left-part">
					<button class="left-button" @click="open">确认验收</button>
				</view>
				<view class="right-part">
					<button class="right-button" @click="back">返回</button>
				</view>
			</view>
			<view v-if="state==='待申请'" class="mix-button">
				<view class="left-part">
					<button class="left-button" @click="addOrder" >立即申请</button>
				</view>
				<view class="right-part">
					<button class="right-button" @click="back">返回</button>
				</view>
			</view>
			<view v-if="status===13" class="mix-button">
				<view class="left-part">
					<button class="left-button" @click="reAddOrder" >重新申请</button>
				</view>
				<view class="right-part">
					<button class="right-button" @click="back">返回</button>
				</view>
			</view>
			<view style="height: 200rpx;"></view>
		</view>
		<uni-popup ref="popup" type="dialog">
		    <uni-popup-dialog mode="input" message="成功消息" title="请进行评价" :duration="2000" :before-close="true" @close="close" @confirm="confirm"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>		

<script>
	import {
		ONLINE_SERVER_ADDRESS_BASE
	} from "../../..//util/network.js"
	import {
		myUploadFile,
		myDownloadFile,
		syncGetOrder,
	} from "../../..//util/network.js"
	import {
		processHistoryList
	} from "../../../util/common.js"
	export default{
		data() {
			return {
				processFlag:false,
				token:'',
				recordId:'',
				processNo:'待分配',
				state: '待受理',
				status: -1,
				selfChecked: false,
				publicChecked: false,
				problemContent:'墙面渗水，尽快修复',
				imageUrl: ['../../../static/add-logo.svg','../../../static/add-logo.svg','../../../static/add-logo.svg',
					'../../../static/add-logo.svg','../../../static/add-logo.svg','../../../static/add-logo.svg',
					0,0,0,0,0,0
					],
				imageValue:[],
				imageTemps:[],
				allPrice: 0,
				chargeImg:'/static/uploadImg.png',                                     //支付信息截图
				chargeImgId: 0,
				comment:'',
				current: 0,
				userMsg:{'userName':'','userPhone':'','userCompany':'','userBuilding':'',
					'userFloor':'','userRoom':''},
				proccessList:[
					{'type':'firstCanvas','commentMsg':'发起申请','time':'2021-01-01 01:01:11'},
					{'type':'middleCanvas','commentMsg':'受理','time':'2020-02-02 02:02:22'},
					{'type':'lastCanvas','commentMsg':'办结','time':'2020-03-03 03:33:33'}
				],
				proccessListDefult:[],
				previewUrl:ONLINE_SERVER_ADDRESS_BASE + '/file/preview/'
			}
		},
		methods: {
			select(e){
				console.log(e)
				e.tempFiles.forEach(item => {
					let a = myUploadFile(this.token, item.path)
					 a.then((v)=>{
						 let temp = {}
						 temp.url = item.url
						 temp.id = v
						 this.imageTemps.push(temp)
						 console.log(this.imageTemps)
					})
				})
			
			},
			deleteImg(e){
				// this.imageTemps.forEach((item, index) => {
				for (var i = 0; i < this.imageTemps.length; i++) {
					if(this.imageTemps[i].url == e.tempFile.url){
						this.imageTemps.splice(i, 1)
						break;
					}
				}
				console.log(this.imageTemps)
			},
			open() {
				this.$refs.popup.open()
			},
			/**
			 * 点击取消按钮触发
			 * @param {Object} done
			 */
			close() {
				// TODO 做一些其他的事情，before-close 为true的情况下，手动执行 close 才会关闭对话框
				// ...
				this.$refs.popup.close()
			},
			/**
			 * 点击确认按钮触发
			 * @param {Object} done
			 * @param {Object} value
			 */
			confirm(value) {
				this.$refs.popup.close()
				if(value == ""){
					uni.showModal({
						content:'评价不能为空',
						showCancel:false
					});
					this.$refs.popup.close()
				}else{
					this.comment = "用户已验收，评价：" + value
					this.modOrder()
					// TODO 做一些其他的事情，手动执行 close 才会关闭对话框
					// ...
					this.$refs.popup.close()
				}
			},
			
			previewPicture(e){
				console.log(this.imageUrl.slice(e,e+1))
				uni.previewImage({
					urls:this.imageUrl.slice(e,e+1),
					longPressActions:{
						itemList:['发送给朋友','保存图片','收藏']
					}
				})
			},
			chooseSelf(){
				if(this.state === '待申请' || this.status===13){
					this.selfChecked = true
				}
			},
			chooseOther(){
				if(this.state === '待申请' || this.status===13){
					this.selfChecked = false
				}
			},
			choosePublic(){
				if(this.state === '待申请' || this.status===13){
					this.publicChecked = true
					this.imageValue = []
				}
			},
			chooseWork(){
				if(this.state === '待申请' || this.status===13){
					this.publicChecked = false
				}
			},
			/*
			功能：支付截图点击事件触发修改或预览照片，如果是待申请 则点击修改照片，否则则是预览
			参数：无
			*/
			chooseImage(index){
				console.log("ceshi",index)
				if(this.state === '待申请' || this.status === 13) {
					uni.chooseImage({
						count: 1,
						sourceType: ['album', 'camera'],
						// #ifdef MP-WEIXIN
						sizeType: ['compressed'],
						// #endif
						success: (res) => {
							let a = myUploadFile(this.token, res.tempFilePaths[0])
							 a.then((v)=>{
								 this.imageUrl[index + 6] = v
							})
							this.imageUrl[index] = res.tempFilePaths[0];
							console.log("ceshi",this.imageUrl[index])
							this.$forceUpdate()
						}
					})
				}else{
					uni.previewImage({
						current: 0,
						urls: [this.imageUrl[index]]
					})
				}
			},
			chooseChargeImg(){
				if(this.status === 7) {
					uni.chooseImage({
						sourceType: ['album', 'camera'],
						// #ifdef MP-WEIXIN
						sizeType: ['compressed'],
						// #endif
						success: (res) => {
							let a = myUploadFile(this.token, res.tempFilePaths[0])
							 a.then((v)=>{
								 this.chargeImgId = v
							})
							this.chargeImg = res.tempFilePaths[0];
							this.$forceUpdate()
						}
					})
				}else{
					uni.previewImage({
						current: 0,
						urls: [this.chargeImg]
					})
				}
			},
			async loadImg(chargeFileId){
				// let _this = this
				const res = await uni.downloadFile({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/file/download/` + chargeFileId,
					header: {
						'Authorization': this.token,
					},
				})
				return new Promise((resolve, reject) => {
				        resolve(res[1].tempFilePath)
				})
			},
			/*
			功能：工单详细页处理通用函数
			参数：
			option：操作类型（先定成 创建、写入、查询）
			*/
		   addOrder(){
		   	console.log("[info]","工单已创建，工单号为：" + this.recordId)
		   	let designDiagramId = this.imageUrl.slice(6,12)
			console.log(designDiagramId[0])
			let status = 0
			let imgArray = []
			for (var i = 0; i < this.imageTemps.length; i++) {
				imgArray.push(this.imageTemps[i].id)
			}
		   	if(!this.publicChecked){
		   		status = 0
				if(imgArray.length == 0){
					uni.showModal({
						content:'请上传设计图',
						showCancel:false
					});
					return 0
				}
		   	}else{
				status = 1
			}
		   	uni.request({
		   		url:`${ONLINE_SERVER_ADDRESS_BASE}/decoration/add`,
		   		header: {
		   			'Authorization': this.token,
		   		},
		   		data: {
		   			designDiagramId: imgArray,
		   			status: status,                        //待写入工单内容
		   			commentMsg: '发起申请'                       //操作信息
		   		},
		   		method:'POST',
		   		success: function (res) {
		   			console.log("提交成功",res.data);
					if(res.data.code == 200){
						uni.navigateBack()
					}
		   		}
		   	})
		   },
		   reAddOrder(){
		   	console.log("[info]","工单已创建，工单号为：" + this.recordId)
		   	let designDiagramId = this.imageUrl.slice(6,12)
		   			console.log(designDiagramId[0])
		   	let status = 0
			
			let imgArray = []
			for (var i = 0; i < this.imageTemps.length; i++) {
				imgArray.push(this.imageTemps[i].id)
			}
		   	if(!this.publicChecked){
		   		status = 0
				if(imgArray.length == 0){
					uni.showModal({
						content:'请上传设计图',
						showCancel:false
					});
					return 0
				}
		   	}else{
				status = 1
			}
		   	uni.request({
		   		url:`${ONLINE_SERVER_ADDRESS_BASE}/decoration/mod/` + this.recordId,
		   		header: {
		   			'Authorization': this.token,
		   		},
		   		data: {
		   			designDiagramId: imgArray,
		   			status: status,                        //待写入工单内容
		   			commentMsg: '发起申请'                       //操作信息
		   		},
		   		method:'POST',
		   		success: function (res) {
		   			console.log("提交成功",res.data);
		   					if(res.data.code == 200){
		   						uni.navigateBack()
		   					}
		   		}
		   	})
		   },
		   confirmDrawing(){
		   	console.log("[info]","确认装修设计图：" + this.recordId)
		   	uni.request({
		   		url:`${ONLINE_SERVER_ADDRESS_BASE}/decoration/confirm-drawing/` + this.recordId,
		   		header: {
		   			'Authorization': this.token,
		   		},
		   		data: {
		   			commentMsg: '用户已确认设计图'                       //操作信息
		   		},
		   		method:'POST',
		   		success: function (res) {
		   			console.log("确认装修设计图成功",res);
					if(res.data.code == 200){
						uni.navigateBack()
					}
		   		},
		   		fail: (res) => {
		   			console.log("确认装修设计图调用失败")
		   		}
		   	})
		   },
			modOrder(){
				console.log("[info]","收到装修服务办结：" + this.recordId)
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/decoration/finish/` + this.recordId,
					header: {
						'Authorization': this.token,
					},
					data: {
						comment:this.comment,
						commentMsg:this.comment,                       //操作信息
					},
					method:'POST',
					success: function (res) {
						console.log("装修服务申请办结成功",res);
						if(res.data.code == 200){
							uni.navigateBack()
						}
					},
					fail: (res) => {
						console.log("装修服务申请办结调用失败")
					}
				})
			},
			cancelOrder(){
				let _this = this
				uni.showModal({
					title: '提示',
					content: '是否确定取消工单？',
					success: function (R) {
						if (R.confirm) {
							console.log('用户点击确定');
							uni.request({
								url:`${ONLINE_SERVER_ADDRESS_BASE}/decoration/cancel/` + _this.recordId,
								header: {
									'Authorization': _this.token,
								},
								data: {
									commentMsg: '用户取消本次装修'                       //操作信息
								},
								method:'POST',
								success: function (res) {
									console.log("用户申请取消成功",res)
									if(res.data.code == 200){
										uni.navigateBack()
									}
								},
								fail: (res) => {
									console.log("用户申请取消调用失败")
								}
							})
						} else if (R.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				console.log("[info]","收到装修服务办结：" + this.recordId)
			},
			getOrder(){
				let a = syncGetOrder("decoration/show/" + this.recordId)
				 a.then((res)=>{
					 console.log("异步",res)
					 if(res.data.code == 200){
						 this.status = parseInt(res.data.data.auditStatus)
						 switch(this.status) {
						 	 case 0:
						 		this.state = "待总监审核"
						 		break;
						 	 case 1:
						 		this.state = "待指派绘图"
						 		break;
						 	 case 2:
						 		this.state = "待工程审核"
						 		break;
						 	 case 3:
						 		this.state = "待绘图上传"
						 		break;
						 	 case 4:
						 		this.state = "待绘图审核"
						 		break;
						 	 case 5:
						 		this.state = "待确认绘图"
						 		break;
						 	 case 6:
						 		this.state = "待总监审核"
						 		break;
						 	 case 7:
						 		this.state = "待指派工程"
						 		break;
						 	 case 8:
						 		this.state = "待工程验收"
						 		break;
						 	 case 9:
						 		this.state = "待客户验收"
						 		break;
						 	 case 10:
						 		this.state = "已验收办结"
						 		break;
						 	 case 11:
						 		this.state = "总监已驳回"
						 		break;
						 	 case 12:
						 		this.state = "已撤回工单"
						 		break;
						 	 case 13:
						 		this.state = "工程已驳回"
						 		break;
						      default:
						 	    this.status = -1
						         this.state = "待申请"
						 } 
							if(res.data.data.status == 0){
								this.selfChecked = false
								this.publicChecked = false
							}else if(res.data.data.status == 1){
								this.selfChecked = false
								this.publicChecked = true
							}else{
								this.selfChecked = true
								this.publicChecked = true
							}
						 this.problemContent = res.data.data.description
						 this.processNo = res.data.data.processOn
						 try{
						 	 var photoArray = res.data.data.designDiagramId.split(",")
							 this.imageValue = []
							 this.imageTemps = []
							 for (var i = 0; i < photoArray.length; i++) {
								let index = i
								let temp = {}
								temp.url = this.previewUrl + photoArray[i]
								temp.id = parseInt(photoArray[i])
								this.imageTemps.push(temp)
								this.imageValue.push(temp)
							 }
						 }catch(e){
						 	//TODO handle the exception
							var photoArray = []
						 }
						 // if(photoArray[0] != 0){
						 // 	for (var i = 0; i < photoArray.length; i++) {
						 // 		let index = i
						 // 		this.imageUrl[index + 6] = photoArray[i]
						 // 		let a = this.loadImg(photoArray[i])
						 // 		a.then((v)=>{
						 // 			this.imageUrl[index] = v
						 // 			this.$forceUpdate()
						 // 		})
						 // 	}
						 // }
						 if(res.data.data.payScreenshotId != null){
						 	let b = this.loadImg(res.data.data.payScreenshotId)
						 	b.then((v)=>{
						 		this.chargeImg = v
						 		this.$forceUpdate()
						 	})
						 }
						 this.proccessListDefult = processHistoryList(res.data.data.processDetail)
						 this.processFlag = true
						 this.$forceUpdate()
					}
				})
			},
			back(){
				uni.navigateBack()
			}
		},
		onLoad(option) {
			//传入单号
			let userInfo = uni.getStorageSync('userinfo')
			if(userInfo != null){
				this.userMsg.userBuilding = userInfo.title
				this.userMsg.userFloor = userInfo.floor
				this.recordId = option.id
				this.state = option.state
				console.log(this.status)
				this.token = uni.getStorageSync('token')

			}else{
				uni.navigateTo('../../login/login')
			}
		},
		mounted() {
			//如果是新建表单则不查询
			if(this.state != '待申请'){
				this.getOrder()
			}
		}
	}
</script>

<style>
	page{
		background-color: #F1F1F1
	}
	.record-id{
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
	}
	.state-bar{
		align-items: center;
		color: #FFFFFF;
		background-color: #DD524D;
		width: 260rpx;
		justify-content: flex-end;
		margin-left: auto;
		margin-right: 50rpx;
		font-size: 30rpx;
		border-radius: 50rpx;
		height: 50rpx;
	}
	.state-msg{
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.in-center{
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	.item-table{
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.radiogroup-style{
		width: 650rpx;
		height: 100rpx;
		background-color: #FFFFFF;
		display: flex;
		margin-left: 50rpx;
		justify-content: center;
		border: solid 1rpx #C0C0C0;
	}
	.textarea-style{
		display: flex;
		justify-content: center;
		width: 650rpx;
		height: 300rpx;
		margin-left: 50rpx;
		background-color: #FFFFFF;
		border: solid 1rpx #C0C0C0;
	}
	.first-radio{
		display: flex;
		height: 100rpx;
		width: 300rpx;
		align-items: center;
	}
	.second-radio{
		display: flex;
		height: 100rpx;
		align-items: center;
		margin-left: 50rpx;
	}
	.mix-button{
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.left-part{
		float: right;
	}
	.right-part{
		float: left;
	}
	.left-button{
		background-color: #FF0000;
		border-radius: 50rpx 0 0 50rpx;
		color: #FFFFFF;
		width: 300rpx;
		font-size: 30rpx;
	}
	.right-button{
		background-color: #0000FF;
		border-radius: 0 50rpx 50rpx 0;
		color: #FFFFFF;
		width: 300rpx;
		font-size: 30rpx;
	}
	.picture-block{
		display: flex;
		flex-direction: column;
		justify-content: center;
		margin-left: 60rpx;
	}
	.picture-part{
		display: flex;
		margin: 0 0 50rpx 0;
		font-size: 100rpx;
		font-weight: 200;
		align-items: center;
		justify-content: center;	
		width: 170rpx;
		height: 170rpx;
		border: dashed 1rpx #000000;
	}
	.picture-style{
		width: 170rpx;
		height: 170rpx;
		margin: 0 0 10rpx 0rpx;
	}
	.add-button {
		border-width: 0px;
		align-items: center;
		justify-content: center;
		margin: 30rpx 0 40rpx 250rpx;
		height: 50rpx;
		width: 250rpx;
		display: flex;
		color: #22B242;
	}
	.payway-style {
		display: flex;
		flex-direction: column;
		width: 650rpx;
	}
	.payway-first-radio {
		height: 100rpx;
		display: flex;
		align-items: center;
		background-color: #FFFFFF;
		padding-left: 25rpx;
		border: solid 0rpx #C8C7CC;
		border-bottom: none;
		border-right: none;
	}
	.payway-second-radio {
		height: 100rpx;
		display: flex;
		align-items: center;
		background-color: #FFFFFF;
		padding-left: 25rpx;
		border: solid 0rpx #C8C7CC;
		border-right: none;
		width: 50rpx;
	}
	.payway-choice {
		display: flex;
		align-items: center;
		background-color: #FFFFFF;
		border: solid 0rpx #C8C7CC;
		width: 600rpx;
		border-left: none;
	}
	.payway-logo {
		width: 50rpx;
		height: 50rpx;
		margin: 0 25rpx 0 25rpx;
	}
	.small-title{
		margin: 20rpx 0 20rpx 50rpx;
	}
	.pay-image{
		margin: 20rpx 0 20rpx 50rpx;
		width: 205rpx;
		height: 205rpx;
	}
	.pay-image-border{
		border:2rpx solid;
		border-radius: 2px;
		margin: 20rpx 0 20rpx 50rpx;
		width: 201rpx;
		height: 201rpx;
	}
	.img-repair{
		margin: 20rpx 50rpx 20rpx 50rpx;
	}
</style>
